package com.collect.discord.jda;

/**
 * @ Author :hky
 * @ Date  : Created in 10:48 2022-09-19
 * @ Version discord
 * @ Decribtion : $
 */
import com.alibaba.fastjson.JSONObject;
import com.collect.common.eventbus.message.EventBusMessage;
import com.collect.common.eventbus.support.EventBusTemplate;
import com.collect.common.redis.RedisCache;
import com.collect.common.redis.RedisKeyUtils;
import com.collect.common.utils.eventbus.EventBusUtils;
import com.collect.common.utils.http.HttpClientUtils;
import com.collect.common.utils.ik.IKAnalyzerSupport;
import com.collect.common.utils.sensitive.SensitiveWordUtils;
import com.collect.common.utils.spring.SpringUtils;
import com.collect.discord.web.domain.BinanceNewestPrice;
import com.collect.discord.web.domain.SensitiveWord;
import com.collect.discord.web.service.IBinancePriceService;
import net.dv8tion.jda.api.OnlineStatus;
import net.dv8tion.jda.api.Permission;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.entities.channel.ChannelType;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

import java.util.*;

public class MessageEvent extends ListenerAdapter {
    @Override
    public void onMessageReceived(@NotNull MessageReceivedEvent event) {
        //如果是机器人发消息触发这个事件则忽略
        //如果不加，则这个事件会变成死循环事件
        if (event.getAuthor().isBot()) {
            return;
        }
        Message message = event.getMessage();
        //获取敏感词
        List<String>  sensitiveList = SensitiveWordUtils.dealIk(message.getContentRaw());
        //异步消息落地
        dealMessage(message);
        if (sensitiveList.size()>0) {
            try {
                //是否探测@事件

                IBinancePriceService binancePriceService = SpringUtils.getBean(IBinancePriceService.class);
                //获取报价信息
                List<BinanceNewestPrice> newestPriceList = binancePriceService.getNewestPriceList(sensitiveList, false);
                String info = JSONObject.toJSONString(newestPriceList);
                message.getChannel().sendMessage(info).queue();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }



    /**
     * 异步发送消息，实现消息落地
     * @param message
     */
    public void dealMessage( Message message){
        //将收集到的消息，异步保存，不影响主进程
        EventBusTemplate eventBusTemplate = SpringUtils.getBean(EventBusTemplate.class);
        eventBusTemplate.asyncPost(new EventBusMessage(EventBusUtils.DISCORDMESSAGE,message,null));
    }

}
